package com.hongxun.design.ui.statisticsmap.pieview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;

/**
 * Created by zerdoor_pc on 2016/6/29.
 */
public class CircularView extends View {
    private Context context;
    private int screenW = -1, screenH = -1;
    private RectF pieOval;
    private Paint  piePaint ;


    private int pieCenterX, pieCenterY, pieRadius;
    public CircularView(Context context) {
        super(context);
        this.context = context;
    }

    public CircularView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CircularView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        screenW = measureWidth(widthMeasureSpec);
        screenH = measureHeight(heightMeasureSpec);
        //设置宽高
        setMeasuredDimension(screenW, screenH);
    }

    //根据xml的设定获取宽度
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        return specSize;
    }

    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        return specSize;
    }
    int color=0;
    @Override
    public void setBackgroundColor(int color) {
        MyLog.L(MString.SUCCESSED + " color=" + color);
          this.color=color;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (color!=0){
            init();
            piePaint.setAntiAlias(true);
            piePaint.setStyle(Paint.Style.FILL);
            piePaint.setColor(context.getResources().getColor(color));
            float sweep = 360;
            canvas.drawArc(pieOval, 0, sweep, true, piePaint);
        }

    }

    private void init() {
        //init screen
//        screenW = ScreenUtils.getScreenW(context);
//        screenH = ScreenUtils.getScreenH(context);
        //if (screenW==-1) return;

        pieCenterX = screenW / 2;
        if (screenW>screenH){
            pieRadius = screenH / 2;
        }else{
            pieRadius = screenW / 2;
        }
        pieCenterY = pieRadius ;

        pieOval = new RectF();
        pieOval.left = pieCenterX - pieRadius;
        pieOval.top = pieCenterY - pieRadius;
        pieOval.right = pieCenterX + pieRadius;
        pieOval.bottom = pieCenterY + pieRadius;
        //The paint to draw circle.
        piePaint = new Paint();
        piePaint.setAntiAlias(true);
        piePaint.setStyle(Paint.Style.FILL);


    }

}
